<?php

namespace app\web\controller;

use app\common\model\Video;
use think\App;
use think\Controller;
use think\Request;
use app\common\model\Order as modelOrder;
class Order extends Controller
{

    private $userInfo;
    public $oss = 'http://oss.adaxiang.com/';
    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->userInfo = $app->request->userInfo;
    }

    public function index($pageSize = 10,$page = 1,$search = [])
    {
	$BTime = '';
	$ETime = '';
        if(empty($search['btime'])){
        //    $BTime =strtotime(date('Y-m-d'));
        }else{
            $BTime = strtotime($search['btime']);
        }
        if(empty($search['etime'])){
        //    $ETime = strtotime(date('Y-m-d',strtotime('+1 day')));
        }else{
            $ETime = strtotime($search['etime']);
        }
	if($BTime && $ETime){
            $search['inputtime1'] = [$BTime,$ETime];
	}
        if(empty($search['status'])) $search['status'] = 1;
        $where = modelOrder::powerWhere(
            $this->userInfo['authority'],
            $this->userInfo['ins_id'],
            $search['status'],
            $this->userInfo['id']);
        $search = array_merge($search,$where);
        $order = ['inputtime1'=>'desc'];
        $data = modelOrder::getList($page,$pageSize,$search,$order);
        return ['code'=>0,'data'=>$data];
    }


    public function save(Request $request)
    {
        $info = input('post.');

        $code = $info['rtuNum'];
        $knockedAlarm = $info['knockedAlarm'];
        if(!$knockedAlarm){
            return ['code'=>1,'msg'=>'设备正常'];
        }
        $equipmentData = model('common/Equipment')->getCode($code);

        if(!$equipmentData){
            return ['code'=>2,'msg'=>'没有找到设备'];
        }

        if($equipmentData['status'] != 1)
        {
            return ['code'=>3,'msg'=>'设备已经是异常状态'];
        }
        file_put_contents('order.log',date('Y-m-d H:i:s').':'.json_encode($info). "\n",FILE_APPEND);
//        $camera = model('common/Equipment')->getType1($equipmentData['ins_id']);
//        $url = '';
//        if($camera){
//            $url = 'https://yc.adaxiang.com/text/one?stream='.$camera['code'].'&start_time='.date('Y-m-d H:i:s');
//            $data = $this->curl($url);
//            $data = json_decode($data,true);
//            $url = '';
//            if($data['data']){
//                if($data['data'][0]['content']['uri']){
//                    $url = $this->oss . $data['data'][0]['content']['uri'];
//                }
//            }
//        }

        $insert['inputtime1'] = time();
        $insert['status'] = 1;
        $insert['equ_id'] = $equipmentData['id'];
        $insert['ins_id'] = $equipmentData['ins_id'];
//        $insert['video'] = $url;
        $ins = model('common/Institution')->get_one($insert['ins_id']);
        $insert['ins_name'] = $ins['name'];
        $insert['culId'] = $equipmentData['culId'];
        $cul = model('common/Culverts')->get_one($insert['culId']);
        $insert['culvertNo'] = $cul['culvertNo'];
        $insert['centerDistance'] = $cul['centerDistance'];

        $insert['number'] = !empty($info['number']) ? $info['number'] : 'Y'.date('ymdHsi');
        $id = modelOrder::addOne($insert);
        $equipmentInfo['status'] = 2;
        $equipmentInfo['orderId'] = $id;
        model('common/Equipment')->edit($equipmentData['id'],$equipmentInfo);
        return ['code'=>0,'msg'=>'添加成功'];
    }

//    public function curl($url){
//        $curl = curl_init();
//        curl_setopt($curl, CURLOPT_URL, $url);
//        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//        $data = curl_exec($curl);
//        curl_close($curl);
//        return $data;
//    }

    public function saveVideo(){
        $str = file_get_contents("php://input");
        //file_put_contents('test.log',$str);
        if(!$str) return ['code'=>1,'msg'=>'失败，没有数据'];
        $data = json_decode($str,true);
        if(!$data) return['code'=>1,'msg'=>'失败，格式化数据失败'];
        $init['uId'] = $data['channel_id'];
        if(!$init['uId']) return['code'=>1,'msg'=>'失败，没有数据'];
        $init['end_time'] = $data['end_time'];
        $init['start_time'] = $data['start_time'];
        $init['video_url'] = $data['video_url'];
        Video::add_one($init);
        return ['code'=>0,'msg'=>'成功'];
    }
    public function saveVideoNew(){
        $str = file_get_contents("php://input");
        //file_put_contents('test1.log',$str);
        if(!$str) return ['code'=>1,'msg'=>'失败，没有数据'];
        $data = json_decode($str,true);
        if(!$data) return['code'=>1,'msg'=>'失败，格式化数据失败'];
        $init['uId'] = $data['stream'];
        if(!$init['uId']) return['code'=>1,'msg'=>'失败，没有数据'];
        $init['end_time'] = $data['stop_time'];
        $init['start_time'] = $data['start_time'];
        $init['video_url'] = 'https://24live.oss-cn-shanghai.aliyuncs.com/'.$data['uri'];
        Video::add_one($init);
        return ['code'=>0,'msg'=>'成功'];
    }



    public function read($id)
    {
        //
        $data = modelOrder::get($id);
        if($data){
	    $img_scene = json_decode($data['img_scene'],true);
	    $data['img_scene'] = explode(',', $img_scene[0]) ? explode(',', $img_scene[0]) :  $img_scene;
            $img_car = json_decode($data['img_car'],true);
            $data['img_car'] = explode(',', $img_car[0]) ? explode(',', $img_car[0]) :  $img_car;
            $data['do_status'] = modelOrder::$status[$data['status']];
            $data['accept_name'] = $this->userInfo['name'];
            $equipmentData = model('common/Equipment')->get_one($data['equ_id']);
            $data['equ_address'] = $equipmentData['address'];
            $data['equ_name'] = $equipmentData['name'];
            $data['lat'] = $equipmentData['lat'];
            $data['lng'] = $equipmentData['lng'];
            if(!$data['ins_name']){
                $ins = model('common/Institution')->get_one($data['ins_id']);
                $data['ins_name'] = $ins['name'];
            }
            $camera = model('Common/EqRelation')->get_ong($data['equ_id']);
//            $camera = model('common/Equipment')->getType1($data['ins_id']);
            $video = modelOrder::getVideo($data['id'],$data['video'],$camera['code'],$data['inputtime1']);
            $data['video'] = $video;
        }
        return ['code'=>0,'data'=>$data];
    }

    public function edit(Request $request, $id)
    {
        $info = $request->param('info');
//        $result = $this->validate(
//            $info,
//            'application\common\validate\Order');
//
//        if (true !== $result) {
//            return ['code'=>1,'msg'=>$result];
//        }
        $data = modelOrder::get($id);
        if(!in_array($data['status'],[1,2])){
            return ['code'=>1,'msg'=>'事件状态异常'];
        }
        if($info['status'] == 2){
            $insert['inputtime2'] = time();
            $insert['accept'] = $this->userInfo['id'];
            $insert['status'] = 2;
            $msg = '受理成功';
        }
        if($info['status'] == 3){
            $insert['inputtime3'] = time();
            $insert['img_scene'] = json_encode($info['img_scene']);
            $insert['img_car'] = json_encode($info['img_car']);
            $insert['level'] = $info['level'];
            $insert['car_id'] = $info['car_id'];
            $insert['name'] = $info['name'];
            $insert['id_card'] = $info['id_card'];
            $insert['phone'] = $info['phone'];
            $insert['note'] = $info['note'];
            $insert['status'] = 3;
            $equ_id = $info['equ_id'];
            $equipmentInfo['status'] = 1;
            $equipmentData = model('common/Equipment')->get_one($equ_id);
            if($equipmentData['status'] != 3){
                model('common/Equipment')->edit($equipmentData['id'],$equipmentInfo);
            }
            $msg = '受理完成';
        }
        if($insert){
            modelOrder::edit($id,$insert);
            return ['code'=>0,'msg'=>$msg];
        }

        return ['code'=>2,'msg'=>'事件状态异常'];
    }

}
